package com.Fengxwgl.controller;

import com.Fengxwgl.entity.Shiming;
import com.Fengxwgl.entity.User;
import com.Fengxwgl.service.impl.UserService;
import com.Fengxwgl.util.R;
import com.Fengxwgl.util.SendSms;
import com.aliyuncs.exceptions.ClientException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
@SessionAttributes("user")
public class UserCon {
    @Resource
    private UserService userService;
    @RequestMapping("/duanxin")
    @ResponseBody
    public R duanxin(String phone,HttpSession httpsession){
        String capture=null;
        try {
            capture = SendSms.sendSms(phone);
            httpsession.setAttribute("phone",phone);
            httpsession.setAttribute("code",capture);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return R.ok("短信发送成功");
    }
    @RequestMapping("/yanzheng")
    @ResponseBody
    public Integer yanzheng (String phone1,String code1,HttpSession httpSession){
        String code = httpSession.getAttribute("code").toString();
        String phone = httpSession.getAttribute("phone").toString();
        if(code==code1||code.equals(code1)&&phone==phone1||phone.equals(phone1)){
            httpSession.removeAttribute("code");
            httpSession.removeAttribute("phone");
            httpSession.setAttribute("phone1",phone);
            return 1;
        }else{
            return 0;
        }
    }
    @RequestMapping("/phone")
    @ResponseBody
    public Integer phone (String phone){
        return userService.phone(phone);
    }
    @RequestMapping("/zhuce")
    @ResponseBody
    public Integer zhuche (String password,HttpSession  session){
        String phone = session.getAttribute("phone1").toString();
        return userService.zhuche(phone,password);
    }
    @RequestMapping("/denglu")
    @ResponseBody
    public Map denglu (String phone, String password, HttpSession httpsession){
        Map denglu = userService.denglu(phone, password);
        httpsession.setAttribute("user",(User)denglu.get("user"));
        return denglu;
    }
    @RequestMapping("/user/userid")
    @ResponseBody
    public Integer useid(HttpSession httpsession){
        User user = (User)httpsession.getAttribute("user");
        Integer uid=user.getUid();
        return uid;
    }
    @RequestMapping("/user/shiming")
    @ResponseBody
    public Integer shiming(Shiming shiming){
        Integer shiming1 = userService.shiming(shiming);
        return shiming1;
    }
    @RequestMapping("/zhuye/userxx")
    @ResponseBody
    public User userxx(HttpSession httpsession){
        User user = (User)httpsession.getAttribute("user");
        return user;
    }
    @RequestMapping("/zhuye/tuichu")
    @ResponseBody
    public Integer tuichu(HttpSession httpsession){
        httpsession.removeAttribute("user");
        return 1;
    }
}